package ar.com.caeycae.dataentry;

import java.util.List;

import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;


public class BBVALoader {

	private WebDriver driver = new FirefoxDriver();
	WebUtil util = new WebUtil();
	private Logger LOGGER = Logger.getLogger(BBVALoader.class);
	
	public void obtenerDatos()
	{
		driver.get("https://www.bbvafrances.com.ar/tlal/jsp/ar/esp/home/index.jsp");
		driver.findElement(By.id("nrodoc")).sendKeys("XXXXXXX");
		driver.findElement(By.id("botonentrar")).click();

		driver.findElement(By.id("usuario")).sendKeys("XXXXXX");
		driver.findElement(By.id("clave")).sendKeys("XXXXXXX");

		driver.findElement(By.className("blueButton")).click();

		closePopUp();
		closePopUp();

		driver.findElement(By.id("rowid1")).click();
		
		util.waitExplorer();
		
		WebElement more = null;
		do{
			more = util.findElement(driver, By.className("more-info"));
			if(more != null && more.isDisplayed()){
				more.click();
				util.waitExplorer();
			}
		}
		while(more != null && more.isDisplayed());
		
		util.waitExplorer();
		
		WebElement tabla = driver.findElement(By.id("tablaMovimientos"));
		List<WebElement> reg = tabla.findElements(By.tagName("tr"));
		for (WebElement webElement : reg) {
			System.out.println(webElement);
		}
		
		//driver.findElement(By.className("desconectar")).click();
	}

	private void closePopUp() {
		util.waitExplorer();
		WebElement modal = util.findElement(driver, By.className("ui-widget-overlay"));
		LOGGER.info("es modal: " + (modal != null));
		if(modal != null)
		{
			WebElement close = util.findElement(driver, By.className("ui-dialog-titlebar-close"));
			if(close != null){
				LOGGER.info("Cerrando popup...");
				close.click();
			}
			else {
				LOGGER.info("Esperando popup...");
				closePopUp();
			}
		}
	}

	
	public static void main(String[] args) {
		new BBVALoader().obtenerDatos();
	}
}
